package com.qingbo.wu.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @Auther: qingbo.wu
 * @Date: 2024/5/29
 * @Description:
 */
public class DruidEncrypteUtil {

    private static String publicKey;

    private static String privateKey;

    static {
        try {
            String[] genKeyPair = ConfigTools.genKeyPair(512);
            privateKey = genKeyPair[0];
            publicKey = genKeyPair[1];
            System.out.println("privateKey :" + privateKey);
            System.out.println("publicKey :" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String plainText) throws Exception {
        return ConfigTools.encrypt(privateKey, plainText);
    }

    public static String decrypt(String encryptText) throws Exception {
        return ConfigTools.decrypt(publicKey, encryptText);
    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("123456");
        System.out.println("encrypt :" + encrypt);
    }
}
